//
//  HJFansVC.m
//  project
//
//  Created by yefei on 2019/7/24.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJFansVC.h"
#import "HJFansCell.h"
#import "HJFansModel.h"

@interface HJFansVC ()
@property (nonatomic,strong) NSMutableArray <HJFansModel *>* dataArray;
@end

@implementation HJFansVC

- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.contro == FansVC) {
        self.navTitle = @"粉丝";
    }else {
        self.navTitle = @"关注";
    }
    [self loadNetworkData];
}

-(void)configTableView {
    self.needRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.emptyView = self.tipEmptyView;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.rowHeight = 66;
}

-(NSMutableArray<HJFansModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(void)loadNetworkData {
    if (self.contro == FansVC) { // 粉丝
        NSString * url = [Api stringByAppendingString:othersConcernPageApi];
        [HJHttpTool get:url params:@{@"pageCurrent":@(self.page)} success:^(HJResponeds *result) {
            NSArray * arr = [NSArray yy_modelArrayWithClass:[HJFansModel class] json:result.data[@"records"]];
            if (self.page==1) {
                [self.dataArray removeAllObjects];
            }
            [self.dataArray addObjectsFromArray:arr];
            [self.tableView reloadData];
            [self.tableView endRefresh];
        } failure:^(id result) {
            [self.tableView endRefresh];
            [YFHud showError:result];
        }];
        return;
    }
    
    // 关注
    NSString * url = [Api stringByAppendingString:myConcernPageApi];
    [HJHttpTool get:url params:@{@"pageCurrent":@(self.page)} success:^(HJResponeds *result) {
        NSArray * arr = [NSArray yy_modelArrayWithClass:[HJFansModel class] json:result.data[@"records"]];
        if (self.page==1) {
            [self.dataArray removeAllObjects];
        }
        [self.dataArray addObjectsFromArray:arr];
        [self.tableView reloadData];
        [self.tableView endRefresh];
    } failure:^(id result) {
        [self.tableView endRefresh];
        [YFHud showError:result];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * identifier = @"fansCell";
    HJFansCell * fansCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!fansCell) {
        fansCell = [[HJFansCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [fansCell loadDataWithModel:self.dataArray[indexPath.row] cellType:self.contro == FansVC ? FansCell : AttenCell];
    @weakify(self)
    fansCell.attentionDidClickBlock = ^(HJFansModel * _Nonnull model) {
        @strongify(self)
        if (self.contro == AttenVC) {
            [self.dataArray removeObject:model];
            [self.tableView reloadData];
        }
        [mNotificationCenter postNotificationName:NotificationAttention object:nil userInfo:@{@"userId":model.user.userId,@"concern":@(model.concern)}];
    };
    return fansCell;
}


@end
